//
//  SelectCountryView.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/4/25.
//

import UIKit

class SelectCountryView: UIView, UITableViewDataSource, UITableViewDelegate {

    typealias SelectCountryViewBlock = (_ countryCode:WLCountryCode) -> Void
    var selectCountryViewBlock : SelectCountryViewBlock!
    
    var dataSource:Array<WLCountryCode>!
//    var sectionAry:Array<Any>!
//    var rowArr:Array<Array<Any>>!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
        self.dataSource = CacheTool.cacheTool.searchCountryCodes()
//        self.rowArr = ContactDataHelper.getFriendListDataBy(self.dataSource)
//        self.sectionAry = ContactDataHelper.getFriendListSectionBy(Array.init(arrayLiteral: self.rowArr!))
        NotificationCenter.default.addObserver(self, selector: #selector(languageNotification), name: NSNotification.Name(rawValue: "languageNotification"), object: nil)
        self.addSubview(self.headerView)
        self.addSubview(self.tableView)
        
        self.headerView.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(0)
            make.right.equalTo(self.snp.right).offset(0)
            make.top.equalTo(self.snp.top).offset(100)
            make.height.equalTo(60)
        }
        
        self.tableView.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(0)
            make.right.equalTo(self.snp.right).offset(0)
            make.bottom.equalTo(self.snp.bottom).offset(0)
            make.top.equalTo(self.headerView.snp.bottom).offset(-15)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func languageNotification()  {
        
    }
    
    func refreshData() {
        if self.dataSource.count <= 0 {
            self.dataSource = CacheTool.cacheTool.searchCountryCodes()
//            self.rowArr = ContactDataHelper.getFriendListDataBy(self.dataSource)
//            self.sectionAry = ContactDataHelper.getFriendListSectionBy(Array.init(arrayLiteral: self.rowArr!))
            self.tableView.reloadData()
        }
    }
    
    @objc func clickOffBtn() {
        self.removeFromSuperview()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return (self.rowArr[section] as AnyObject).count
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "SelectCountryIdentifier")!
//        let model:WLCountryCode = (self.rowArr[indexPath.section][indexPath.row]) as! WLCountryCode
        let model:WLCountryCode = self.dataSource[indexPath.row]

        cell.textLabel?.text = model.nicename + model.phonecode
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if (selectCountryViewBlock != nil) {
//            selectCountryViewBlock(self.rowArr[indexPath.section][indexPath.row] as! WLCountryCode)
            selectCountryViewBlock(self.dataSource[indexPath.row])
        }
        self.removeFromSuperview()
    }
    
//    func numberOfSections(in tableView: UITableView) -> Int {
//        tableView.tableViewDisplayMessage(message: "没有信息", rowCount: self.dataSource.count)
////        return self.rowArr.count
//        return self.dataSource.count
//    }
    lazy var headerView:UIView = {
        let headerView = UIView()
        headerView.layer.cornerRadius = 15
        headerView.layer.masksToBounds = true
        headerView.backgroundColor = UIColor.white
        
        let btn:UIButton = UIButton.init(type: .custom)
        btn.setImage(UIImage.init(named: "login_cancel"), for: .normal)
        btn.addTarget(self, action: #selector(clickOffBtn), for: .touchUpInside)
        headerView.addSubview(btn)
        
        btn.snp.makeConstraints { (make) in
            make.right.equalTo(headerView.snp.right).offset(-15);
            make.height.equalTo(13);
            make.width.equalTo(13);
            make.centerY.equalTo(headerView.snp.centerY).offset(-5);
        }
        
        let lab:UILabel = UILabel()
        lab.text = "Select your country"
        headerView.addSubview(lab)
        lab.textColor = UIColor.init(hexString: "#3A3A3A")
        lab.font = UIFont.systemFont(ofSize: 19)
        lab.textAlignment = .center
        lab.snp.makeConstraints { (make) in
            make.height.equalTo(20);
            make.centerX.equalTo(headerView.snp.centerX).offset(0);
            make.centerY.equalTo(headerView.snp.centerY).offset(-5);
        }
        return headerView
    }()
    
    lazy var tableView:UITableView = {
        let tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "SelectCountryIdentifier")
        tableView.tableFooterView = UIView()
        return tableView
    }()
    
}
